package com.ctg.itrdc.imageconsole.control.image;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ctg.itrdc.imageconsole.common.bean.CommonResult;
import com.ctg.itrdc.imageconsole.common.bean.ReturnCodeDefine;
import com.ctg.itrdc.imageconsole.data.dto.image.MicroUserDTO;
import com.ctg.itrdc.imageconsole.data.dto.image.RepositoryDTO;
import com.ctg.itrdc.imageconsole.data.dto.image.TagShareDTO;
import com.ctg.itrdc.imageconsole.data.entity.DockerImageTag;
import com.ctg.itrdc.imageconsole.service.image.ImageService;
import com.ctg.itrdc.paspsdk.common.entity.PageInfo;


@Controller
@RequestMapping(value = "/image")
public class ImageHarborController {

	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Resource
	ImageService imageService;

	@RequestMapping(value = "/repositories", method = RequestMethod.GET)
	@ResponseBody
	public CommonResult getRepositories(
			@RequestParam("imageType") String imageType, 
			@RequestParam("sysUserCode") String sysUserCode, 
			@RequestParam(value="imageName", required=false) String imageName, 
			@RequestParam("pageNum") Integer pageNum,
			@RequestParam("pageSize") Integer pageSize) {
		try {
			PageInfo<RepositoryDTO> repositories = imageService.getRepositories(imageType, sysUserCode, pageNum, pageSize);
			return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000, repositories);
		} catch (Exception e) {
			return new CommonResult(ReturnCodeDefine.Code.C1200, e.getMessage(), null);
		}
	}

	@RequestMapping(value = "/repository/tags", method = RequestMethod.GET)
	@ResponseBody
	public CommonResult getTags(@RequestParam("imageType") String imageType, @RequestParam("imageName") String repository) {
		try {
			List<DockerImageTag> tags = imageService.getTags(imageType, repository);
			return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000, tags);
		} catch (Exception e) {
			return new CommonResult(ReturnCodeDefine.Code.C1200, e.getMessage(), null);
		}
	}


	@RequestMapping(value = "/repository/tag/share", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult tagShare(@RequestBody TagShareDTO tagShareDTO) {
		try {
			imageService.tagShare(tagShareDTO);
			return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000);
		} catch (Exception e) {
			logger.error("/repository/tag/share 接口(post请求)出现异常！",e);
			return new CommonResult(ReturnCodeDefine.Code.C1200, e.getMessage(), null);
		}
	}
	
	
	/**
	 * 获取镜像版本的所有共享用户信息
	 * @param dockerImageTagId 镜像版本的主键id
	 * @return
	 */
	@RequestMapping(value = "/repository/tag/share", method = RequestMethod.GET)
	@ResponseBody
	public CommonResult getTagShare(@RequestParam("dockerImageTagId") Long dockerImageTagId) {
		try {
			List<MicroUserDTO> shareDTOs = imageService.getTagShares(dockerImageTagId);
			return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000, shareDTOs);
		} catch (Exception e) {
			logger.error("/repository/tag/share 接口(get请求)出现异常！",e);
			return new CommonResult(ReturnCodeDefine.Code.C1200, e.getMessage(), null);
		}
	}
	
	
}
